package com.city.statisticsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.city.commonutils.Result;
import com.city.statisticsservice.entity.StatisticsDaily;
import com.city.statisticsservice.feign.UcenterClient;
import com.city.statisticsservice.mapper.StatisticsDailyMapper;
import com.city.statisticsservice.service.StatisticsDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author city
 * @since 2020-10-10
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {


    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public StatisticsDaily getCount(String day) {
        Result result = ucenterClient.countRegister(day);
        int count = (int)result.getData().get("count");
        //写入数据库
        //不可能每次查都写入数据库，数据库应该存每天都只有一条，都是最新的数据，所以判断该日期有没有数据，有则更改，无则添加，或删除后添加
        //调用很多接口来获取各方面的数据

        QueryWrapper<StatisticsDaily> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("statistics_daily",day);
        StatisticsDaily one = baseMapper.selectOne(queryWrapper);
        if (one==null){
            StatisticsDaily statisticsDaily = new StatisticsDaily();
            statisticsDaily.setDateCalculated(day);
            //注册人数
            statisticsDaily.setRegisterNum(count);
            //statisticsDaily.setVideoViewNum();
            //statisticsDaily.setLoginNum();
            //statisticsDaily.setCourseNum();
            baseMapper.insert(statisticsDaily);
            return statisticsDaily;
        }else {
            one.setDateCalculated(day);
            //注册人数
            one.setRegisterNum(count);
            //one.setVideoViewNum();
            //one.setLoginNum();
            //one.setCourseNum();
            baseMapper.updateById(one);
            return one;
        }
    }
}
